<template>
  <div class="box">
    <hu-card class="pt20">
      <div @click="back" class="box_quit">
        <img src="@/assets/images/back.png" alt="" />
      </div>
      <el-form :model="queryParams" label-width="100px">
        <el-row :gutter="50">
          <el-col :span="6">
            <el-form-item label="分类">
              <el-select v-model="queryParams.typeId" placeholder="请选择分类">
                <el-option
                  v-for="dict in dict.type.branch_column_type"
                  :key="dict.value"
                  :value="dict.value"
                  :label="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item class="searchFromButton">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="HuSearchList"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-left" @click="HuResetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="10" class="mb10 space_between" type="flex">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
            >新增</el-button
          >
          <el-button
            type="danger"
            @click="handleAllDelete"
            icon="el-icon-delete"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <hu-table
        :tableData="tableData"
        :loading="HuLoading"
        :tableCol="tableCol"
        @selection-change="handleSelectionChange"
      >
        <!-- 文件 -->
        <template v-slot:files="{ row }">
          <!-- 多个文件 -->
          <HuFileBut
            v-for="(item, index) in row.files"
            :key="index"
            :path="item.previewPath"
            :name="item.originalName"
          />
        </template>
        <template v-slot:operate="{ row }">
          <el-button type="text" @click="handleUpdate(row, 0)">查看</el-button>
          <el-button type="text" @click="handleUpdate(row, 2)">修改</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </hu-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </hu-card>

    <hu-drawer
      :open.sync="showModal"
      :setLook="HusetLook"
      :loading="HuLoading"
      @submit="addFrom"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="rulesSon"
        :disabled="HusetLook === 0"
        label-width="100px"
      >
        <el-form-item label="分类" prop="typeId">
          <el-select v-model="formData.typeId" placeholder="请选择分类">
            <el-option
              v-for="dict in dict.type.branch_column_type"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="附件" prop="files">
          <f-upload
            :fileList.sync="formData.files"
            @remove="removeFile"
          ></f-upload>
        </el-form-item>
      </el-form>
    </hu-drawer>
  </div>
</template>
<script>
import * as http from "@/api/模板";
export default {
  dicts: ["branch_column_type"],
  data() {
    return {
      loading: false,
      showModal: false, // 弹窗
      // list 参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0, // table total
      // table Col
      tableCol: [
        {
          type: "selection",
          width: "55",
        },
        {
          title: "序号",
          type: "index",
          width: "55",
        },
        {
          title: "分类",
          key: "typeName",
        },
        {
          title: "附件",
          slot: "files",
        },
        {
          title: "党支部",
          key: "partyBranchName",
        },
        {
          title: "创建人",
          key: "cuserName",
        },
        {
          title: "创建时间",
          key: "ctime",
        },
        {
          title: "操作",
          slot: "operate",
          width: "200",
          fixed: "right",
        },
      ],
      tableData: [], // table Data
      // form Data
      formData: {
        id: undefined,
      },
      // 表单校验
      rulesSon: {
        typeId: [
          { required: true, message: "分类不能为空", trigger: "change" },
        ],
        files: [{ required: true, message: "附件不能为空", trigger: "change" }],
      },
      ids: [], // table 勾选项
      // 部门
      dictsList: [],
      // 班组
      teamsList: [],
      teamsFormList: [],
    };
  },
  created() {
    this.getDicts();
    this.getList();
  },
  methods: {
    // get 部门
    getDicts() {
      this.$http.getDicts().then(({ result }) => {
        this.dictsList = result;
      });
    },
    // query change 部门
    deptChange() {
      this.$set(this.queryParams, "groupId", undefined);
      this.getTeams();
    },
    // query get 班组
    getTeams() {
      this.loading = true;
      let params = this.queryParams.deptId && [this.queryParams.deptId];
      this.$http.getTeams(params).then(({ result }) => {
        this.loading = false;
        this.teamsList = result;
      });
    },
    // 列表 List
    getList() {
      this.HuLoading = true;
      let params = {
        ...this.queryParams,
        branchColumnId: this.$route.query.branchColumnId,
      };
      http.getList(params).then(({ result }) => {
        this.HuLoading = false;
        this.tableData = result.records;
        this.total = result.total;
      });
    },
    // form change 部门
    deptFormChange() {
      this.$set(this.formData, "groupId", undefined);
      this.getFormTeams();
    },
    // form get 班组
    getFormTeams() {
      this.loading = true;
      let params = this.formData.deptId && [this.formData.deptId];
      this.$http.getTeams(params).then(({ result }) => {
        this.loading = false;
        this.teamsFormList = result;
      });
    },
    // 删除文件
    removeFile(arr) {
      if (!arr[0].raw) {
        if (!this.formData.removeFileIds) {
          this.formData.removeFileIds = [];
        }
        this.formData.removeFileIds.push(arr[0].id);
      }
    },
    // 表单重置
    reset() {
      this.HusetLook = "";
      this.formData = {
        id: undefined,
      };
      this.resetForm("formData");
    },
    // add 弹出
    handleAdd() {
      this.reset();
      this.HusetLook = 1;
      this.showModal = true;
    },
    // Update 弹出
    handleUpdate(row, status) {
      this.reset();
      this.HusetLook = status;
      this.formData = {
        id: row.id,
        typeId: row.typeId,
        files: row.files,
      };
      this.getFormTeams();
      this.showModal = true;
    },
    // 提交
    addFrom() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$startLoading();
          // 提交 formData
          let params = new FormData();
          Object.keys(this.formData).forEach((e, i) => {
            if (this.formData[e]) {
              if (e === "files") {
                this.formData[e].forEach((item) => {
                  if (!item.id) {
                    params.append(e, item.raw);
                  }
                });
              } else {
                params.append(e, this.formData[e]);
              }
            }
          });
          params.append("branchColumnId", this.$route.query.branchColumnId);
          if (this.formData.id != undefined) {
            http
              .updateList(params)
              .then(() => {
                this.$modal.msgSuccess("修改成功");
                this.showModal = false;
                this.getList();
              })
              .finally(() => {
                this.$endLoading();
              });
          } else {
            http
              .addList(params)
              .then(() => {
                this.$modal.msgSuccess("新增成功");
                this.showModal = false;
                this.HuSearchList();
              })
              .finally(() => {
                this.$endLoading();
              });
          }
        } else {
          this.$nextTick(() => {
            this.$utils.scrollIntoView();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除数据项？")
        .then(() => {
          this.$startLoading();
          return http.deleteList({ ids: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.$endLoading();
        });
    },
    // 多选框 table
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },
    // 删除勾选
    handleAllDelete() {
      if (!this.ids.length) return this.$modal.msg("请勾选表格");
      this.$modal
        .confirm("是否确认删除数据项？")
        .then(() => {
          this.$startLoading();
          return http.deleteList({ ids: this.ids.join(",") });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.$endLoading();
        });
    },
    back() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
